package fr.ensisa.sunspot.actor.writer.strategy;

import fr.ensisa.sunspot.actor.writer.media.IMedia;
import fr.ensisa.sunspot.event.IEvent;
/**
 *
 * @author Michel Hassenforder
 */

public class DelayedWriter extends AbstractWriteStrategy {

    private IEvent record [];
    private int count = 0;

    public DelayedWriter (int size, IMedia media) {
        super (media);
        this.record = new IEvent [size];
        this.count = 0;
    }

    public void setActivated (boolean activated) {
        if (activated) {
            count = 0;
        }
        super.setActivated(activated);
    }

    public void write (long timeStamp, IEvent incomingEvent) {
        if (count < record.length) {
            record[count++] = incomingEvent.clone();
        }
        if (count == record.length) {
            for (int i=0; i < record.length;++i) {
                IEvent event = record[i];
                if (event != null) {
                    getMedia().writeHeader (timeStamp, incomingEvent);
                    getMedia().writeEvent (timeStamp, incomingEvent);
                    getMedia().writeTrailer (timeStamp, incomingEvent);
                }
                event = null;
                record[i] = null;
            }
            count=0;
        }
    }

}
